const express = require('express');
const router = express.Router();
const url = require('url');
const sqlQuery = require('./maisisql');
const config = require('./config')
const userData =require('./data/mine/mine')
const yinshi =require('./data/Found/Featured/Diet/index')
const jingxuans =require('./data/Found/Featured/Find/jinxuans')
const yinshis =require('./data/Found/Featured/Diet/indexs')
const jwt = require('jsonwebtoken')
const jingxuan = require('./data/Found/Featured/Find/jinxian')
const Home = require('./data/Home/home')
const sportData = require('./data/sport/sport')
const courseData = require('./data/course/course')
const Paiming = require('./data/Home/paiming')
const Ituidao =require('./data/Found/Dynamics/Ituidao')
const Concern =require('./data/Found/Dynamics/Concern')
const Record=require('./data/course/mycourse')
const Runjilu = require('./data/Home/runjilu')
const Nearby =require('./data/Found/Dynamics/Nearby')
const Shop =require('./data/Found/Featured/Shop')
const Zan = require('./data/Found/Dynamics/Ituidao/zan')
// 解决跨域
router.all("*", function (req, res, next) {
    //设置允许跨域的域名，*代表允许任意域名跨域
    res.header("Access-Control-Allow-Origin", "*");
    //允许的header类型
    res.header("Access-Control-Allow-Headers", "content-type");
    //跨域允许的请求方式 
    res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
    if (req.method.toLowerCase() == 'options')
        res.send(200); //让options尝试请求快速结束
    else
        next();
})


//登录
router.post('/login', (req, res) => {
    let username = req.body.username;
    let password = req.body.password;
    let sql = 'select * from login where username=? and password=?';
    let arr = [username, password];
    console.log(username, password)
    sqlQuery(sql, arr, function (result) {
        if (result.length > 0) {
            const token = jwt.sign({
                id: result[0].id,
                username: result[0].username
            }, config.jwtSecret)
            res.send({
                success: true,
                msg:'登陆成功',
                token
            })
        } else {
            res.send({
                msg: '用户名密码错误',
                success: false
            })
        }
    })
})
//注册
router.post('/register', (req, res) => {
    let username = req.body.username;
    let password = req.body.password;
    let sql = 'select * from login where username=?';
    let arr = [username];
    sqlQuery(sql, arr, function (data) {
        // res.send(data)
        if (data.length > 0) {
            res.send({
                msg: '用户名已存在',
                success: false
            })

        } else {
            let sql2 = 'insert into login values ("",?,?,"")'
            let arr2 = [username, password];
            sqlQuery(sql2, arr2, (result) => {
                if (result.affectedRows > 0) {
                    res.send({
                        success: true,
                        msg: '注册成功'
                    })
                } else {
                    res.send({
                        success: false,
                        msg: '注册失败'
                    })
                }
            })
        }
    })

})

//获取用户信息
router.get('/user', (req, res) => {
    res.send({
        success: true,
        userData
    })
})
//获取精选图片
router.get('/jingxuan', (req, res) => {
    res.send({
        success: true,
        jingxuan
    })
})
router.get('/jingxuans', (req, res) => {
    res.send({
        success: true,
        jingxuans
    })
})
//获取饮食图片
router.get('/yinshi', (req, res) => {
    res.send({
        success: true,
        yinshi
    })
})
//获取点赞头像图片
router.get('/zan', (req, res) => {
    res.send({
        success: true,
        Zan
    })
})

//获取发现页面商城信息
router.get('/shop', (req, res) => {
    res.send({
        success: true,
        Shop
    })
})
router.get('/yinshis', (req, res) => {
    res.send({
        success: true,
        yinshis
    })
})
//获取首页信息
router.get('/home', (req, res) => {
    res.send({
        success: true,
        Home
    })
})
// 获取首页排名信息
router.get('/paiming', (req, res) => {
    res.send({
        success: true,
        Paiming
    })
})
// 获取首页跑步记录信息
router.get('/runjilu', (req, res) => {
    res.send({
        success: true,
        Runjilu
    })
})

//获取教练信息
router.get('/sportCoach', (req, res) => {
    res.send({
        success: true,
        sportData
    })
})
//获取课程信息
router.get('/course', (req, res) => {
    res.send({
        success: true,
        courseData
    })
})
//获取动态健身圈信息
router.get('/ituidao',(req,res)=>{
    let id=url.parse(req.url,true).query.id;
    console.log(url.parse(req.url,true).query.id);
    if(id){
        let arr=Ituidao.filter(item=>{
            return item.id == id 
        })
        console.log(arr);
        
        res.send({
            success:true,
            arr
        })
    }else{

        res.send({
            success:true,
            Ituidao
        })
    }
})
// 获取动态详情数据
router.get('/content',(req,res)=>{
    res.send({
        success:true,
        Content
    })
})
//获取动态附近信息
router.get('/nearby',(req,res)=>{
    res.send({
        success:true,
        Nearby
    })
})
//获取动态关注信息
router.get('/concern',(req,res)=>{
    res.send({
        success:true,
        Concern
    })
})
module.exports=router;

//获取我的课程信息
router.get('/record',(req,res)=>{
    res.send({
        success:true,
        Record
    })
})




module.exports=router;
